$(function() {
		load_charts = function(type){
			$('#charts-body ul').css('display','none');
			var loaded = false;
			var currentDiv = null;
			$('#top-photo-nav li').removeClass('active');
			switch(type) {
			    case 'WEEK':
			    	currentDiv = $('#charts-week');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#top-photo-nav li')[0]).addClass('active');
			        break;
			    case 'MONTH':
			    	currentDiv = $('#charts-month');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#top-photo-nav li')[1]).addClass('active');
			        break;
			};
			if(!loaded){
				$('#charts-loader').fadeIn();
				$.get("/charts/" + type, function(data) {
					$('#charts-loader').fadeOut();
					for (var i = 0; i < data.length; ++i) {
						var obj = data[i];
						var html = "<li><span class='number special-" + (i+1) +"'>"+ (i+1) + "</span>	<div class='info_data'> ";
							html += "<div class='photo-thumbnail'> <img src='/resources/data/photos/thumbnails/" + obj.troll.url +"'> </div> ";
							html += "<a href='/photo/" + obj.troll.id + "' class='title'>" + obj.troll.title + "</a> " +
								   "<div> <a href='/uploader/" + obj.troll.uploader.id + "' class='name_uploader' title=''target='_blank'>" + obj.troll.uploader.name+ "</a> </div>" +
								   "<span class='views-small' title='lượt xem'>" + obj.troll.viewNumber.format() +
								   "</span> <span class='comments-small' title='lượt bình luận'>" + obj.troll.commentNumber.format() + "</span>" +
								   "</div></li>";
						$(currentDiv).append(html);
					};
					if($("#charts-body .viewMore").length == 0){
						$("#charts-body").append("<div class='viewMore'><a href='/bang-xep-hang'>Xem thêm >>></a></div>");
					}
				}, 'json').fail(function(e, data) {
				});
			}
		};
		
		load_full_charts = function(type){
			$('#bxh-content .bxh-content-detail').css('display','none');
			var loaded = false;
			var currentDiv = null;
			$('#bxh-type-nav li').removeClass('active');
			switch(type) {
			    case 'WEEK':
			    	currentDiv = $('#bxh-content-week');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#bxh-type-nav li')[0]).addClass('active');
			        break;
			    case 'MONTH':
			    	currentDiv = $('#bxh-content-month');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#bxh-type-nav li')[1]).addClass('active');
			        break;
			};
			if(!loaded){
				$('#bxh-loader').fadeIn();
				$.get("/bang-xep-hang/" + type, function(data) {
					$('#bxh-loader').fadeOut();
					for (var i = 0; i < data.length; ++i) {
						var obj = data[i];
						var html = "<div class='photoListItem'>	<div class='listItemSeparator'></div><span class='number special-"+ (i + 1)+"'>" + (i + 1)+"</span> "
								+  "<div class='photoThumbnail'><a href='/photo/" + obj.troll.id + "' target='_blank'> <img src='/resources/data/photos/thumbnails/"
								+  obj.troll.url + "' alt='" + obj.troll.title + "' class='thumbImg'></a></div><div class='info'> "
								+  "<h2><a href='/photo/" + obj.troll.id + "'>" + obj.troll.title + "</a></h2><div class='uploader'>Đăng bởi <a href='/uploader/" + obj.troll.uploader.id + "'>" + obj.troll.uploader.name + "</a></div> "
								+  "<div class='stats'><div class='viewComments'><span class='views' title='lượt xem'>" + obj.troll.viewNumber.format() + "</span> "
								+  "<span class='comments' title='lượt bình luận'>" + obj.troll.commentNumber.format() + "</span>	<div class='likesWrapper'><fb:like class='fb-like' "
								+  "data-href='/photo/" + obj.troll.id + "' data-width='90' data-layout='button_count' data-action='like' data-show-faces='false' data-share='true'></fb:like>"
								+  "</div></div></div><div class='Z-Chart'>" + obj.total + "</div></div>";
						$(currentDiv).append(html);
					};
					FB.XFBML.parse();
				}, 'json').fail(function(e, data) {
				});
			}
		};
		
		load_top_uploader = function(type){
			$('#top-uploader-body .top-uploader').css('display','none');
			var loaded = false;
			var currentDiv = null;
			$('#top-uploader-nav li').removeClass('active');
			switch(type) {
			    case 'WEEK':
			    	currentDiv = $('#top-uploader-week');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#top-uploader-nav li')[0]).addClass('active');
			        break;
			    case 'MONTH':
			    	currentDiv = $('#top-uploader-month');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#top-uploader-nav li')[1]).addClass('active');
			        break;
			    case 'ALL':
			    	currentDiv = $('#top-uploader-all');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#top-uploader-nav li')[2]).addClass('active');
			        break;
			};
			if(!loaded){
				$('#uploader-loader').fadeIn();
				$.get("/uploader/top-uploader/"+ type, function(data) {
					$('#uploader-loader').fadeOut();
					for (var i = 0; i < data.length; ++i) {
						var obj = data[i];
						var html = "<div class='item'><a href='/uploader/" + obj.id + "'><img src='"+ obj.avatar + "'><div class='info'> "
			                     + "<span class='name'>" + obj.name +"</span> <span class='likes-small'>" + obj.likeNumber.format() + "</span></div></a></div>";
						$(currentDiv).append(html);
					};
					if($("#top-uploader-body .viewMore").length == 0){
						$("#top-uploader-body").append("<div class='viewMore'><a href='/uploader/top'>Xem thêm >>></a></div>");
					}
				}, 'json').fail(function(e, data) {
				});
			}
			
		};
		
		load_full_top_uploader = function(type){
			$('.top-dh-content').css('display','none');
			var loaded = false;
			var currentDiv = null;
			$('#top-type-nav li').removeClass('active');
			switch(type) {
			    case 'WEEK':
			    	currentDiv = $('#top-dh-week');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#top-type-nav li')[0]).addClass('active');
			        break;
			    case 'MONTH':
			    	currentDiv = $('#top-dh-month');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#top-type-nav li')[1]).addClass('active');
			        break;
			    case 'ALL':
			    	currentDiv = $('#top-dh-all');
			    	if($.trim($(currentDiv).html()) != ""){
			    		loaded = true;
			    	}
			    	$(currentDiv).css('display','block');
			    	$($('#top-type-navli')[2]).addClass('active');
			        break;
			};
			if(!loaded){
				$('#top-dh-loader').fadeIn();
				$.get("/uploader/top/"+ type, function(data) {
					$('#top-dh-loader').fadeOut();
					for (var i = 0; i < data.length; ++i) {
						var obj = data[i];
						var html = "<div class='item'><a href='/uploader/" + obj.id +"'><img src='" + obj.avatar + "'> "
								  + "<div class='info'> <span class='name'>" + obj.name + "</span> <span class='likes'>" + obj.likeNumber.format() + "</span> "
								  + "</div></a></div>";
						$(currentDiv).append(html);
					};
				}, 'json').fail(function(e, data) {
				});
			}
			
		};
		
		Number.prototype.format = function(n, x) {
		    var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
		    return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&.');
		};
});